﻿Imports System
Imports System.Collections
Imports System.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend Class DynamicPropertyDescriptor
        Inherits PropertyDescriptor
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal descriptor As PropertyDescriptor)
            MyBase.New(descriptor, Nothing)
            Me.serviceProvider = serviceProvider
            Me.realPropertyDescriptor = descriptor
        End Sub

        Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
            Return Me.realPropertyDescriptor.CanResetValue(component)
        End Function

        Public Overrides Function GetValue(ByVal component As Object) As Object
            If (component Is Nothing) Then
                Return Nothing
            End If
            Return Me.realPropertyDescriptor.GetValue(component)
        End Function

        Public Overrides Sub ResetValue(ByVal component As Object)
            Me.realPropertyDescriptor.ResetValue(component)
        End Sub

        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            If TypeOf component Is IComponent Then
                Me.realPropertyDescriptor.SetValue(component, value)
            Else
                PropertyDescriptorUtils.SetPropertyValue(Me.ServiceProvider, Me.realPropertyDescriptor, component, value)
            End If
        End Sub

        Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
            Return (String.Equals(Me.realPropertyDescriptor.GetType.FullName, "System.ComponentModel.Design.InheritedPropertyDescriptor", StringComparison.Ordinal) OrElse Me.realPropertyDescriptor.ShouldSerializeValue(component))
        End Function


        ' Properties
        Public Overrides ReadOnly Property Attributes As AttributeCollection
            Get
                Dim list As New ArrayList
                list.AddRange(Me.realPropertyDescriptor.Attributes)
                list.Add(New MergablePropertyAttribute(False))
                Return New AttributeCollection(DirectCast(list.ToArray(GetType(Attribute)), Attribute()))
            End Get
        End Property

        Public Overrides ReadOnly Property Category As String
            Get
                Return Me.realPropertyDescriptor.Category
            End Get
        End Property

        Public Overrides ReadOnly Property ComponentType As Type
            Get
                Return Me.realPropertyDescriptor.ComponentType
            End Get
        End Property

        Public Overrides ReadOnly Property Converter As TypeConverter
            Get
                Return Me.realPropertyDescriptor.Converter
            End Get
        End Property

        Public Overrides ReadOnly Property Description As String
            Get
                Return Me.realPropertyDescriptor.Description
            End Get
        End Property

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return Me.realPropertyDescriptor.DisplayName
            End Get
        End Property

        Public Overrides ReadOnly Property IsReadOnly As Boolean
            Get
                Return Me.realPropertyDescriptor.IsReadOnly
            End Get
        End Property

        Public Overrides ReadOnly Property PropertyType As Type
            Get
                Return Me.realPropertyDescriptor.PropertyType
            End Get
        End Property

        Public ReadOnly Property RealPropertyDescriptor As PropertyDescriptor
            Get
                Return Me.realPropertyDescriptor
            End Get
        End Property

        Public ReadOnly Property ServiceProvider As IServiceProvider
            Get
                Return Me.serviceProvider
            End Get
        End Property


        ' Fields
        Private realPropertyDescriptor As PropertyDescriptor
        Private serviceProvider As IServiceProvider
    End Class
End Namespace

